package com.qf.blog.controller;

import com.qf.blog.commons.exception.BusinessException;
import com.qf.blog.commons.response.ResponseResult;
import com.qf.blog.commons.security.UserTokenThreadLocal;
import com.qf.blog.enums.ExceptionEnum;
import com.qf.blog.enums.LikeActionEnum;
import com.qf.blog.service.LikeService;
import com.qf.blog.vo.LikeVo;
import com.qf.blog.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping ("/like")
public class LikeController {
    @Autowired
    private LikeService likeServiceImpl;

    /**
     * 点赞/取消点赞
     *
     * @param likeVo
     * @author Zhendong Song
     * @time 2023/6/8 20:10
     **/
    @PostMapping ("/")
    @ResponseBody
    public ResponseResult handleLike(@RequestBody LikeVo likeVo) {
        UserToken userToken = UserTokenThreadLocal.get();
        if (userToken == null) {
            throw new BusinessException(ExceptionEnum.NOT_LOGIN);
        }
        Integer userId = userToken.getUserId();
        likeVo.setUserId(userId);
        if (LikeActionEnum.LIKE.getValue() == likeVo.getActionType()) {
            likeServiceImpl.like(likeVo);
        } else {
            likeServiceImpl.unlike(likeVo);
        }
        return ResponseResult.success();
    }
}
